package org.weasel.business;

import java.sql.Time;
import java.util.ArrayList;

import org.weasel.data.ProjectManager;

public class Release extends Item {
	private String id;
	private String name;
	private ArrayList<Iteration> iterations;
	private Unscheduled unscheduled;
	private ProjectManager manager;

	public Release(String id, String name, ArrayList<Iteration> iterations, Unscheduled unscheduled, ProjectManager manager) {
		this.id = id;
		this.name = name;
		if(iterations == null) {
			this.iterations = new ArrayList<Iteration>();
		} else {
			this.iterations = iterations;
		}
		this.unscheduled = unscheduled;
		this.manager = manager;
	}
	

	public Iteration createAndAddIteration(String name) throws Exception {
		Iteration i = manager.createIteration(this.id, name);
		iterations.add(i);
		return i;
	}
	
	public Iteration getIteration(String iterationID) {
		for (Iteration iteration : iterations) {
			if(iteration.getId().equals(iterationID)) {
				return iteration;
			}
		}
		return null;
	}
	
	public void removeIteration(String iterationID) throws Exception {
		for (int i = 0; i < iterations.size(); i++) {
			if(iterations.get(i).getId().equals(iterationID)) {
				//remove also from XML
				manager.deleteIteration(iterationID);
				iterations.remove(i);
			}
		}
	}

	public ArrayList<Iteration> getIterations() {
		return iterations;
	}

	public void setIterations(ArrayList<Iteration> iterations) {
		this.iterations = iterations;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public UserStory getUnscheduledStory(String storyID){
		return unscheduled.getUnscheduledUserStory(storyID);
	}
	
	@Deprecated
	public UserStory addUnscheduledStory(String name, int businessValue, Time estimatedTime, Time usedTime, int effort) throws Exception{
//		UserStory story = manager.createUserStory(this.unscheduled.getId(), name, businessValue, estimatedTime, usedTime, effort);
		//TODO check this here...because the line above would create the story and also the Unscheduled object
		UserStory story = unscheduled.addUnscheduledUserStory(new UserStory("123", name, businessValue,estimatedTime, usedTime, effort,null,null));
		return story;
	}
	
	@Deprecated
	public void removeUnscheduledStory(String storyID) throws Exception{
		//TODO should be needed here, since it is done by the Unscheduled object
		//		manager.deleteUserStory(storyID);
		unscheduled.removeUnscheduledUserStory(storyID);
	}
	
	public Unscheduled getUnscheduled() {
		return unscheduled;
	}

	public void setUnscheduled(Unscheduled unscheduled) {
		this.unscheduled = unscheduled;
	}
	

}
